package com.atguigu.day22.tcpsocket.s1;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.PrintStream;
import java.net.InetAddress;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.Scanner;

import static java.lang.System.in;

public class Client {
    public static void main(String[] args) throws IOException {
        System.out.println("------------Client-------------");

        //1.创建客户端对象
        //todo 当前的客户端会向 本机  端口号是 9999 的服务端发送信息
     //   Socket socket = new Socket("localhost",9999);
        Socket socket = new Socket(InetAddress.getLocalHost(),9999);

        //2.获取字节输出流
        OutputStream os = socket.getOutputStream();
        //3.转为打印流
        PrintStream ps = new PrintStream(os);
        //4.写出数据
        ps.println("你好吗 我的朋友");




        //获取字节输入流
        InputStream is = socket.getInputStream();
        //创建Scanner
        Scanner sc = new Scanner(is);
        //读取数据并展示
        String str = sc.nextLine();
        System.out.println("服务器发来的信息是: " + str);
        //5.关闭资源
        ps.close();
        os.close();
        socket.close();
    }
}
